package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;


import com.alibaba.fastjson.JSON;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckMap;
import com.itheima.service.MapService;


import com.itheima.utils.QiniuUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/map")
public class MapController {

    @Reference
    private MapService mapService;
    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult result = mapService.pageQuery(queryPageBean);
        return result;
    }

    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            mapService.delete(id);
            return new Result(true, MessageConstant.DELETE_ADDRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.DELETE_ADDRESS_FAIL);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody CheckMap checkMap) {
        try {
            mapService.add(checkMap);

            return new Result(true, MessageConstant.ADD_ADDRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.ADD_ADDRESS_FAIL);
    }


    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            CheckMap checkMap = mapService.findById(id);
            if (checkMap != null) {
                return new Result(true, MessageConstant.CALLBACK_ADDRESS_SUCCESS, checkMap);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.CALLBACK_ADDRESS_FAIL);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckMap checkMap) {
        try {
            mapService.edit(checkMap);
            return new Result(true, MessageConstant.UPDATE_ADDRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.UPDATE_ADDRESS_SUCCESS);
    }


    /*@RequestMapping("/location")
    public Result location(String address, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("接受" + address);

        try {
            HttpSession session = request.getSession();
            session.setAttribute("longitude", 111.11);
            session.setAttribute("latitude", 53.333);
            return new Result(true, "success");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "fail");
    }*/

    @RequestMapping(path = "/location", method = RequestMethod.GET)
    @ResponseBody
    public String location(String address) throws UnsupportedEncodingException {
        /** 通过传入的address地址获取经纬度逻辑
         */
        byte[] bytes = address.getBytes("ISO-8859-1");
        String s = new String(bytes, "utf-8");
        System.out.println(s);
        Map<String, String> result = new HashMap<>();
        result.put("place", s);
        System.out.println(JSON.toJSONString(result));


        /*String longitude = "136.11";
        String latitude = "37.333";
        Map<String,String> result = new HashMap<>();
        result.put("longitude",longitude);
        result.put("latitude",latitude);
        System.out.println(JSON.toJSONString(result));*/
        return JSON.toJSONString(result);

    }


    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile) {
        //获取原始文件名
        String originalFilename = imgFile.getOriginalFilename();
        //获取文件后缀
        int lastIndexof = originalFilename.lastIndexOf(".");
        //生成新的文件名
        String suffix = originalFilename.substring(lastIndexof);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSSS");
        Date date = new Date();
        String fileName = sdf.format(date) + "冬酱QAQ" + suffix;
        try {
            QiniuUtils.upload2Qiniu(imgFile.getBytes(), fileName);
            Result result = new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS);
            result.setData(fileName);
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES, fileName);
            return result;
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }

    }
    @RequestMapping(path = "/findAllAddress", method = RequestMethod.GET)
    @ResponseBody
    public Result findAllAddress() {
        List<CheckMap> list = mapService.findAllAddress();
        System.out.println(list.size());
        for (CheckMap checkMap : list) {
            System.out.println(checkMap);
        }
        if (list!=null){
            return new Result(true,"success",list);
        }else {
            return new Result(false,"fail");
        }
    }

}

















